package com.best.onegous.http;


import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import com.best.onegous.viewutils.ToastView;
import com.best.onegous.viewutils.WuWangLuo;


import android.content.Context;
import android.view.Gravity;


public class BaseModel implements BusinessResponse{

    protected ArrayList<BusinessResponse > businessResponseArrayList = new ArrayList<BusinessResponse>();
    protected Context mContext;
    

    public BaseModel()
    {

    }
    
    public BaseModel(Context context)
    {
        mContext = context;
    }
    
    protected void saveCache()
    {
        return ;
    }

    protected void cleanCache()
    {
        return ;
    }

    public void addResponseListener(BusinessResponse listener)
    {
        if (!businessResponseArrayList.contains(listener))
        {
            businessResponseArrayList.add(listener);
        }
    }

    public void removeResponseListener(BusinessResponse listener)
    {
        businessResponseArrayList.remove(listener);
    }

    //公共的错误处理
    public boolean callback(String url, JSONObject jo)
    {
    	
    	System.out.println("main jo --> "+jo);
        if (null == jo)
        {
            WuWangLuo toast = new WuWangLuo(mContext,"网络错误，请检查网络设置");
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            return false;
        }
        try {
			if("0".equals(jo.getString("status"))){
				
			  ToastView toast = new ToastView(mContext, jo.getString("info"));
              toast.setGravity(Gravity.CENTER, 0, 0);
              toast.show();
              return false;
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
      

    }


	@Override
	public void OnMessageResponse(String url, JSONObject jo) throws JSONException {
		// TODO Auto-generated method stub
		 for (BusinessResponse iterable_element : businessResponseArrayList)
	        {
	            iterable_element.OnMessageResponse(url,jo);
	        }
	}
}
